package com.huadi.firematerialident.controller;

import com.huadi.firematerialident.po.Appraisal;
import com.huadi.firematerialident.po.Review;
import com.huadi.firematerialident.service.AppraisalServiceImpl;
import com.huadi.firematerialident.service.ReviewServiceImpl;
import com.huadi.firematerialident.service.ReviewerServiceImpl;
import com.huadi.firematerialident.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/rev")
public class ReviewController {

    @Autowired
    ReviewServiceImpl reviewService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    ReviewerServiceImpl reviewerService;
    @Autowired
    AppraisalServiceImpl appraisalService;

    @RequestMapping("/getAllRev")
    @ResponseBody
    public String getAll(Model model){
        List<Map<String,Object>> list = reviewService.getAllReview();
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/getMyRev")
    @ResponseBody
    public String getMy(Model model){
        List<Map<String,Object>> list = reviewService.getMyReview();
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/review")
    public String addReview( @RequestParam("ApsID") String ApsID,
                             @RequestParam("RevStatus") String RevStatus,
                             @RequestParam("ArchivingStatus") String ArchivingStatus,
                             @RequestParam("RevConclusion")  String RevConclusion,
                             Model model){

        String role=userService.getRole();
        if(!role.equals("reviewer")) {
            model.addAttribute("msg","您没有权限!");
            return "/app/appcommit";
        }

        //判断鉴定表中是否有该数据，如果有，正常执行逻辑，如果没有，则提示错误
        List<Map<String,Object>> list = appraisalService.selectAppraisalByID(ApsID);
        if(list.size() == 0){
            model.addAttribute("msg","请查看已经提交的鉴定表，检查您输入的鉴定表id是否正确");
            return "/rev/apslist";
        }

        //判断该鉴定表是有没有提交过审查，如果提交过，则报错，提示不能重复提交审查！
        List<Map<String,Object>> list1 = reviewService.selectReviewByID1(ApsID);
        if(list1.size() !=0){
            model.addAttribute("msg","该鉴定表不能重复审查！");
            return "/rev/apslist";
        }

        Appraisal appraisal = appraisalService.selectAppraisalByID_class(ApsID);
        System.out.println("con"+appraisal.getApsID());
        appraisal.setRevStatus(RevStatus);
        System.out.println(appraisal.getRevStatus());
        appraisalService.updateAppraisal(appraisal);

        Review review = new Review();
        review.setReviewID();
        review.setApsID(ApsID);
        review.setRevID(reviewerService.getRevID());
        review.setRevConclusion(RevConclusion);
        review.setArchivingStatus(ArchivingStatus);
        reviewService.addReview(review);

        model.addAttribute("msg","提交成功！");

        return "/rev/revlist";
    }

    @RequestMapping("/revDel")
    public String delReview(String revID,Model model){
        String role=userService.getRole();
        if(!role.equals("review")&&(reviewService.selectReviewByID_class(revID)).getReviewID().equals(reviewerService.getRevID())) {
            model.addAttribute("msg","您没有权限!");
            return "/rev";
        }
        if(reviewService.deleteReviewByID(revID)!=0){
            model.addAttribute("msg", "删除成功");
        }
        else {
            model.addAttribute("msg", "删除失败");
        }
        return "/rev";
    }

    @RequestMapping("/revUpdate")
    public String updateReview(@RequestParam("ReviewID") String ReviewID,
                               @RequestParam("ApsID") String ApsID,
                               @RequestParam("RevTime") LocalDateTime RevTime,
                               @RequestParam("RevConclusion") String RevConclusion,
                               @RequestParam("ArchivingStatus") String ArchivingStatus,
                               Model model){
        String RevID = reviewerService.getRevID();
        Review review =  reviewService.selectReviewByID_class(ReviewID);
        if(!review.getRevID().equals(RevID)){
            model.addAttribute("msg","您没有权限");
            return "/rev/revlist";
        }

        review.setReviewID(ReviewID);
        review.setApsID(ApsID);
        review.setRevTime(RevTime);
        review.setRevConclusion(RevConclusion);
        review.setArchivingStatus(ArchivingStatus);

        if(reviewService.updateReview(review)==0){
            model.addAttribute("msg","修改失败！");
        }
        else{
            List<Map<String,Object>> list = reviewerService.selectReviewerByID(review.getRevID());
            model.addAttribute("msg","修改成功！");
        }
        return "/rev/revlist";
    }

    @RequestMapping("/revSel")
    public String selectReview(String reviewid, Model model){

        String role=userService.getRole();
        if(!role.equals("review")) {
            model.addAttribute("msg","您没有权限!");
            return "/rev";
        }

        Review rev = reviewService.selectReviewByID_class(reviewid);
        if(rev==null) {
            model.addAttribute("msg", "没有查找到该表！");
        }
        else {
            model.addAttribute("msg", "查询成功！");
            model.addAttribute("review",rev);
        }
        return "/rev/list";
    }


}
